package com.seocoo.worker.friend;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.seocoo.worker.R;
import com.seocoo.worker.base.http.BasePageRsp;
import com.seocoo.worker.base.refresh.SwipeRefreshMoreLayout;
import com.seocoo.worker.base.widget.EditTextCloseView;
import com.seocoo.worker.friend.adapter.FriendSearchAdapter;
import com.seocoo.worker.order.entity.RspOrderIWorkmateInfo;
import com.seocoo.worker.order.model.OrderRelevanceWorkmateModel;
import com.sfh.lib.event.BusEventManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/3/719:49
 * desc   : 搜索工友
 * version: 1.0
 */
public class FriendSearchActivity extends MVVMActivity implements BaseQuickAdapter.OnItemClickListener {

    private FriendSearchAdapter mAdapter = new FriendSearchAdapter();
    private SwipeRefreshMoreLayout swipe_refresh_more_layout;

    public static void start(Context context, List<RspOrderIWorkmateInfo> data) {
        Intent intent = new Intent(context, FriendSearchActivity.class);
        intent.putExtra("data", (Serializable) data);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_search_activity);
        List<RspOrderIWorkmateInfo> data = (List<RspOrderIWorkmateInfo>) getIntent().getSerializableExtra("data");
        mAdapter.setHaveData(data);
        swipe_refresh_more_layout = findViewById(R.id.swipe_refresh_more_layout);
        ImageView iv_back = findViewById(R.id.iv_back);
        TextView tv_commit = findViewById(R.id.tv_commit);
        tv_commit.setOnClickListener(v -> {
            SparseArray<RspOrderIWorkmateInfo> tempData = mAdapter.getTempData();
            if (tempData.size() == 0) {
                showToast("请至少选择一个工友");
                return;
            }
            ArrayList<RspOrderIWorkmateInfo> list = new ArrayList<>();
            for (int i = 0; i < tempData.size(); i++) {
                RspOrderIWorkmateInfo workmateInfo = tempData.valueAt(i);
                list.add(workmateInfo);
            }
            Gson gson = new Gson();
            String json = gson.toJson(list);
            BusEventManager.postEvent(json);
            finish();
        });
        iv_back.setOnClickListener(v -> finish());
        EditTextCloseView et_input = findViewById(R.id.et_input);
        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //&& s.length() == 11
                if (!TextUtils.isEmpty(s)) {
                    getViewModel(OrderRelevanceWorkmateModel.class).queryFriendList(1, s.toString());
                } else {
                    getViewModel(OrderRelevanceWorkmateModel.class).queryFriendList(1, "");
                }
            }
        });
        swipe_refresh_more_layout.setAdapter(mAdapter, true);
        swipe_refresh_more_layout.setOnItemClickListener(this);
        swipe_refresh_more_layout.setOnLoadingListener(nowPage -> {
            swipe_refresh_more_layout.onLoadAllCompale();
        });
        //进来就刷新列表
        getViewModel(OrderRelevanceWorkmateModel.class).queryFriendList(1, "");
    }

    @LiveDataMatch
    public void onFriendList(BasePageRsp<RspOrderIWorkmateInfo> data) {
        swipe_refresh_more_layout.onRefreshCompale(data);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        mAdapter.setTempData(position);
    }
}
